<?php 
namespace slinks\extensions\amf;

use slinks\core\DependencyInjection\Reference;

use slinks\common\addendum\Addendum;

use slinks\common\addendum\ReflectionAnnotatedClass;

use slinks\core\DependencyInjection\Definition;

use slinks\core\DependencyInjection\ContainerBuilder;

use slinks\core\DependencyInjection\Extension\Extension;

/**
 * Provides @RemoteMethod annotation support.  
 * 
 * @author Navid Mitchell
 *
 */
class ZendAmfExtension extends Extension{
	
	public function configLoad($config, ContainerBuilder $container){
		
		if(!$container->hasDefinition('zend.amf')){
		
			include_once 'annotation/RemoteMethod.php';
		
			// register the shortcut Annotation names with the Addendum 
			Addendum::registerClass('RemoteMethod','\slinks\extensions\amf\annotation\RemoteMethod');
			
			// Set up definition for SlinksAmfController
			$proxyDoctrineCollections = false;
			if(isset($config['proxyDoctrineCollections'])){
				$proxyDoctrineCollections = $config['proxyDoctrineCollections'];
			}
			
			$controllerDef = new Definition('slinks\extensions\amf\zend\SlinksAmfController',
											array($config['zendDir'],$proxyDoctrineCollections));	
																		
			$controllerDef->setFactoryMethod('create');
			$controllerDef->addMethodCall('setContainer',array(new Reference('slinks.container')));
	
			// find any remote method annotations and add them to the ZendAmfController config. 
			foreach($container->getDefinitions() as $id => $definition){
				
				if (null !== $definition->getFile()) {
            		require_once $container->getParameterBag()->resolveValue($definition->getFile());
        		}
        		
        		$remoteMethods = array();
        		
				$r = new ReflectionAnnotatedClass($container->getParameterBag()->resolveValue($definition->getClass()));
				foreach($r->getMethods() as $method){
					if($method->hasAnnotation('RemoteMethod')){
						$remoteMethods[] = $method->getName();
					}
				}
				
				if(count($remoteMethods) > 0){
					$controllerDef->addMethodCall('addRemoteMethods', array($id,$remoteMethods));
				}
			}
			
			$controllerDef->addMethodCall('prepareServer');
			
			$container->setDefinition('zend.amf', $controllerDef);
		}
		
	}
	
    /**
     * Returns the base path for the XSD files.
     *
     * @return string The XSD base path
     */
    public function getXsdValidationBasePath()
    {
         return __DIR__.'/schema';
    }

    /**
     * Returns the namespace to be used for this extension (XML namespace).
     *
     * @return string The XML namespace
     */
    public function getNamespace()
    {
        return 'http://www.mitchellsoftware.net/schema/dic/zendamf';
    }

    /**
     * Returns the recommended alias to use in XML.
     *
     * This alias is also the mandatory prefix to use when using YAML.
     *
     * @return string The alias
     */
    public function getAlias()
    {
        return 'zendamf';
    }
	
	
}


